<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有!                                |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | Revision: 1.0 $                                                        |
// +------------------------------------------------------------------------+
// | Author: anguolei@clcw.com.cn                                           |
// +------------------------------------------------------------------------+
//


namespace AuctionBack\Controller;

use AuctionCore\Broker\Broker;
use AuctionCore\Common\AjaxPage;
use AuctionCore\Account\FrontUser;
/**
 * 红包管理
 * @author angl
 * @package AuctionBack\Controller
 */
class RedPacketsController extends BaseController  {

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->display();
    }

    public function getList()
    {
        $page = I("request.p", 1);
        $pageSize = 10;
        $t = I('request.t/d');
        $uid = I("request.uid", '');
        $phone = I('request.mobile','');
        $params = [
            'uid'=>$uid,
            'phone'=>$phone
        ];
        $list = Broker::getList($page, $pageSize , $uid, $phone );
        $Page = new AjaxPage($list['count'], $pageSize,"RedPacket.p");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $page_data = $Page->show();
        $this->assign("t",$t);
        $this->assign('p', $page);
        $this->assign('list',$list);
        $this->assign('params',$params);
        $this->assign('page_data',$page_data);
        $this->display();
    }

    public function withdrawList()
    {
        $page = I("request.p", 1);
        $pageSize = 10;
        $t = I('request.t/d',0);
        $uid = I("request.uid", '');
        $phone = I('request.mobile', '');
        $real_name = I('request.real_name', '');
        $id_card = I('request.id_card','');
        $start_time = I('request.start_time', '');
        $end_time = I('request.end_time', '');
        $ret_arr = [2=>'0',3=>'1',4=>'2'];
        $result = $ret_arr[$t];
        $params = [
            'uid' => $uid,
            'phone' => $phone,
            'real_name' => $real_name,
            'id_card' => $id_card,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'result' => $result[$t]
        ];
        $list = Broker::getWithdrawList($page, $pageSize, $result, $uid, $phone, $real_name, $id_card, $start_time, $end_time);
        $Page = new AjaxPage($list['count'], $pageSize,"RedPacket.p");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $page_show = $Page->show();
        $this->assign("t", $t);
        $this->assign('p', $page);
        $this->assign('params', $params);
        $this->assign('page_show', $page_show);
        $this->assign('list', $list);
        $this->display();
    }

    //提现
    public function withdraw()
    {
        $id = I('get.id/d',0);
        if($id<1){
            $this->helpJsRedirect("参数不正确");
        }
        $withdraw_info = Broker::getWithdrawInfoById($id);
        if(empty($withdraw_info)){
            $this->helpJsRedirect("未找到提现记录");
        }
        $broker = Broker::getDetailByAid($withdraw_info['aid']);
        $user_data = FrontUser::get_user_by_uid($broker['uid']);
        $this->assign('withdraw_info',$withdraw_info);
        $this->assign('user_data',$user_data);
        $this->display();
    }

    public function doWithdraw()
    {
        $id = I('post.id/d',0);
        if($id<1){
            $this->helpJsonResult(400,'参数错误');
        }
        $result = I('post.result');
        if($result<1){
            $this->helpJsonResult(400,'请选择提现结果');
        }
        $remark = I('post.remark');
        if($result==2 && empty($remark)){
            $this->helpJsonResult("请选择提现失败原因");
        }
        $withdraw_info = Broker::getWithdrawInfoById($id);
        if(empty($withdraw_info)){
            $this->helpJsonResult(404,'未找到提现记录');
        }
        $ret = Broker::upWithdrawById((int)$id, ['emp_id'=>$this->user_info['emp_id'],'result'=>(int)$result,'remark'=>trim($remark)]);
        if(!$ret){
            $this->helpJsonResult(500,"数据更新失败或未更新");
        }
        //如果是提现失败,改支出为收入,wallet+回去
        if($result==2){
            Broker::upWalletByAid($withdraw_info['aid'],$withdraw_info['amount'] );
            \AuctionCore\Broker\Commission::updateLog($withdraw_info['cl_id'], ['type'=>1,'commission_type'=>5]);
        }
        $this->helpJsonResult(200, '更新成功');
    }

    public function batchWithdraw()
    {
        $ids = I('post.ids');
        if(empty($ids)){
            $this->helpJsonResult(400,"请选择一条记录");
        }
        $ret = Broker::batchWithdrawByIds($ids,['result'=>1,'emp_id'=>$this->user_info['emp_id']]);
        if(!$ret){
            $this->helpJsonResult(500,"数据更新失败或未更新");
        }
        $this->helpJsonResult(200, '更新成功');
    }

}

//end class